<?php

namespace App\Http\Controllers\Api;

use Illuminate\Http\Request;
use App\Http\Controllers\Controller;

use App\Http\Requests\Api\ApiRequest;
use App\Services\RedisService;
use App\Services\Idcard;
use App\Services\AMap;
use App\Repos\UserRepo;
use App\Repos\InviteRepo;
use App\Repos\FavoriteRepo;
use App\Repos\QuestionRepo;
use App\Models\Page;


class IndexController extends Controller
{
     public function getToken(Request $request)
    {
        $rs['status'] = true;
        
        $mobile = $request->mobile;
        

        $user = UserRepo::find(['mobile'=>$mobile]);
        if($user) {
            $rs['data']['token']   = $user->remember_token;
            $rs['data']['user_id'] = $user->id;
            return response()->json($rs);
        }

        $data = array_only($request->all(),['longitude','latitude','mobile']); 
        $data['remember_token'] = str_random(16);      
        $user =UserRepo::create($data);
        $rs['data']['token']   = $user->remember_token;
        $rs['data']['user_id'] = $user->id;
        return response()->json($rs);   
    }

    // 筛选用户
    public function getUsers(ApiRequest $request)
    {
        $rs['status'] = true;
        $user_id      = $request->user_id;
        

        $list = UserRepo::queryIntendUser($user_id);
        if(empty($list)) {
            $rs['status'] = false;
            $rs['msg'] = '没有找到合适的用户信息';
            return response()->json($rs);     
        }
        $rs['data'] = $list;
        return response()->json($rs);
        
    }

    // 筛选用户
    public function srcUsers(ApiRequest $request)
    {
        $rs['status'] = true;
        $user_id      = $request->user_id;
        $map  = $request->pure();
        if( !$map['height'][0]) {
            array_pull($map,'height');
        } else {
            $map['height'] = ltrim(implode(',', $map['height']),',');  
        }

        if( !$map['age'][0]) {
            array_pull($map,'age');
        } else {
            $map['age'] = ltrim(implode(',', $map['age']),',');
        }
        

        $list = UserRepo::queryIntendUser($user_id,$map);
        if(empty($list)) {
            $rs['status'] = false;
            $rs['msg'] = '没有找到合适的用户信息';
            return response()->json($rs);     
        }
        $rs['data'] = $list;
        return response()->json($rs);
        
    }

    public function getUserInfo(ApiRequest $request)
    {
        $rs['status'] = true;
        $to_id = (int)$request->id;
        
        //查看用户是否被邀约通过 如果被邀约通过了信息不打码
        // TA 要约我
        $invite = [];
        if($request->invite_id) {
            $where['id'] = (int)$request->invite_id;
            $invite = InviteRepo::find($where);
        } else {
            $where['from_user_id'] = $request->user_id;
            $where['to_user_id']   = $to_id;
            $invite = InviteRepo::find($where);
        }

        

        

        $fwhere['user_id']          = $request->user_id;
        $fwhere['favorite_user_id'] = $to_id;
        $favorite = FavoriteRepo::find($fwhere);
      
        //dd($invite);
        if(object_get($invite,'status')==100 ) {
            $data = UserRepo::find(['id'=>$to_id]);
            $data->idcard = substr_replace($data->idcard, '******', -6,6);
        } else {
            $data = UserRepo::getUserInfo(['id'=>$to_id]);   
        }

        if($data) {
            $data->remember_token = '';
            $data->idcard_a = '';
            $xing = substr($data->truename,0,3);      
            $data->chenghu =  $data->sex=='男'?$xing.'先生':$xing.'女士';
            $data->truename = '';  
        }

        $qwhere['to_user_id'] = $to_id;
        $qwhere['i.status']     = 1;
        $qas = QuestionRepo::getPages($qwhere,10);
        
        

        $rs['data'] = $data;
        $rs['data']['favorited'] = 0;
        if($favorite) {
            $rs['data']['favorited'] = 1;
        }
        $rs['data']['invite'] =  $invite;        
        $rs['data']['qas']    = $qas;
        return response()->json($rs);


    }

    public function getIdcardInfoA(ApiRequest $request)
    {
        $rs['status'] = false;
        $filePath =  ltrim($request->filepath,'/');
        // 正面信息
        $data = Idcard::getIdcardInfo_A($filePath); 
        
        if(empty($data['idcard'])){
            $rs['msg'] = '身份证文件解析失败';
            return response()->json($rs);
        }    
        //检查身份证是否存在
        $where['idcard'] = $data['idcard'];

        $user = UserRepo::find($where);
        if($user){
            $rs['msg'] = '身份证号码已经存在';
            return response()->json($rs);
        }

        $map['id']        = $request->user_id;
        $data['idcard_a'] = $filePath;
        $flag = UserRepo::update($map,$data);
        if($flag) {
            $rs['status'] = true;
            $rs['data'] = array_except($data,'idcard_a');
            return response()->json($rs);
        }
        return response()->json($rs);
    }

     public function getIdcardInfoB(ApiRequest $request)
    {
        $rs['status'] = false;
        $filePath     = ltrim($request->filepath,'/');        

        // 背面信息
        $data             = Idcard::getIdcardInfo_B($filePath);
        $data['idcard_b'] = $filePath;
        $map['id']        = $request->user_id;

        $flag = UserRepo::update($map,$data);
        if($flag) {
            $rs['status'] = true;
            return response()->json($rs);
        }
        return response()->json($rs);
    }

    // 发起邀约
    public function invite(ApiRequest $request)
    {
        $rs['status'] = false;
        if(!$request->to_user_id) {
            $rs['msg'] = '邀约对象数据异常';
            return response()->json($rs);
        }
        $data['from_user_id'] = $request->user_id;
        $data['to_user_id']   = $request->to_user_id;        
        $data['face_score']   = $request->face_score;
        $data['point']        = (int)$request->use_point;

        $where = array_only($data,['from_user_id','to_user_id']);
        
        // 检测之前是否邀约过
        $invite = InviteRepo::findOne($where);
        if(object_get($invite,'status')==1 || object_get($invite,'status')==100) {
            $rs['msg'] = '邀约中';
            return response()->json($rs);
        }

        $data['face_score'] = object_get($request,'user.face_score');
        $flag = InviteRepo::save($data);
        if($flag['status']==true) {
            $rs['status'] = true;
            return response()->json($rs);
        } else {
            $rs['msg'] = $flag['msg'];
        }
        return response()->json($rs);
    }

    public function ganzhi()
    {
        $rs['status'] = true;
        $arr = ['甲子', '甲戌', '甲申', '甲午','甲辰', '甲寅',
                '乙丑', '乙亥', '乙酉', '乙未','乙巳','乙卯',
                '丙寅', '丙子', '丙戌', '丙申','丙午','丙辰',
                '丁卯', '丁丑', '丁亥', '丁酉','丁未','丁巳',
                '戊辰', '戊寅', '戊子', '戊戌','戊申','戊午',
                '己巳', '己卯', '己丑', '己亥','己酉','己未',
                '庚午', '庚辰', '庚寅', '庚子','庚戌','庚申',
                '辛未', '辛巳', '辛卯', '辛丑','辛亥','辛酉',
                '壬申', '壬午', '壬辰', '壬寅','壬子','壬戌',
                '癸酉', '癸未', '癸巳', '癸卯','癸丑','癸亥'];
        $rs['data'] = $arr;
        return response()->json($rs);        
    }

    public function coordToAddress(ApiRequest $request)
    {
        $rs['status'] = false;
        $coord = $request->coord;
        $info = Amap::coordiateToAddress($coord);
        if($info['status'] = true) {
            $rs['status'] = true;
            $rs['data']   = $info['address'];
            return response()->json($rs);
        }
        return response()->json($rs);
    }

    public function aboutUs(ApiRequest $request)
    {   
        $rs['status'] = true;
        $rs['data'] = Page::find(1);
        return response()->json($rs);
    }

    public function reg(Request $request)
    {
        
        
        $data['nickname'] = $request->nickname;
        $data['remember_token'] = str_random(16);
        $user = UserRepo::createTest($data);
        if($user) {
            $rs['status'] = true;           
            $rs['data']['token']   = $user->remember_token; 
            $rs['data']['user_id'] = $user->id;
            return response()->json($rs);   
        }
        $rs['status'] = false;
        $rs['msg'] = '注册失败';
        return response()->json($rs);
    }
}
